package java07;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Main040 {

    public static void main(String[] args) throws InterruptedException {
        ExecutorService exec = Executors.newCachedThreadPool();
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();//num of tasks
        List<CalculateTask> taskList = new ArrayList<CalculateTask>();
        List<Future<Integer>> results = new ArrayList<Future<Integer>>();
        int sum = 0;
        for (int i = 1; i <= n; i++) {
        	CalculateTask task = new CalculateTask(i);
        	taskList.add(task);
		}
       
        try {
            /*invokeAll批量运行所有任务, submit提交单个任务*/
        	results = exec.invokeAll(taskList);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            /*从future中输出每个任务的返回值*/
            for (Future<Integer> future : results) {
//                System.out.println(future.get());//get方法会阻塞直到结果返回
                sum += future.get();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        exec.shutdown();
        System.out.println("sum="+sum);
        
	}
}
class CalculateTask implements Callable<Integer>{
	private int n;
	
	public CalculateTask(int n){
		this.n = n;
	}
	@Override
	public Integer call() throws Exception {
		return fib(n);
	}
	public Integer fib(int m ){
		if(m == 0)
			return 0;
		if(m == 1)
		    return 1;
		return fib(m-1) + fib(m - 2);
	}
	
}
